﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FantasyDraft2.Models;

namespace FantasyDraft2.Controllers
{
    public class InjuryController : Controller
    {
        FantasyDraftEntities fantasyDB = new FantasyDraftEntities(); 

        //
        // GET: /Injury/
        public ActionResult Index()
        {
            PlayerInjuryViewModel model = new PlayerInjuryViewModel
            {
                Players = fantasyDB.Players.ToList(),
                playerId = 0,
                isInjured = "0"
            };
            return View(model);
        }

        //
        // POST: /Injury/
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(PlayerInjuryViewModel model)
        {
            bool injuredResult = false; //This will be used to conver the results passed from the View.  Convert 1 to True and 0 to False.
            var currentPlayer = fantasyDB.Players.Find(model.playerId);
            PlayerEvent newEvent = new PlayerEvent
            {
                EventType = 2,
                InitialTeamId = currentPlayer.TeamId,
                FinalTeamId = currentPlayer.TeamId, 
                PlayerId = model.playerId,
                EventDate = DateTime.Now
            };
            fantasyDB.PlayerEvents.Add(newEvent);

            //If returned value is true, set player's IsInjured to true and set IsActive to false.  If false, set IsInjured to false.
            if (model.isInjured == "1")
            {
                injuredResult = true;
                currentPlayer.IsInjured = injuredResult;
                currentPlayer.IsActive = false; 
            }
            else
            {
                injuredResult = false;
                currentPlayer.IsInjured = injuredResult; 
            }
            fantasyDB.Entry<Player>(currentPlayer).State = System.Data.EntityState.Modified;
            fantasyDB.SaveChanges();

            
            return RedirectToAction("Index", "Admin");
        }

    }
}
